﻿/*
	Copyright 2009 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	MP3tunes Functions
*/

var sid;
var partnertoken = 2989248581;
	var isCover = false;

function mp3tunes(){

	var request = null;



/**************************************************************
	login to a mp3tunes account
*************************************************************/
	mp3tunes.prototype.mp3_login = function(user, pwd){
		anim.startLodingAnimation();
		var requestString = "https://shop.mp3tunes.com/api/v1/login?output=json&username=" +user+ "&password=" + pwd + "&partner_token=" + partnertoken + "&callback=mp3_setSessionID";
		this.sendRequest(requestString, false);
	}

/*********************************************************
retrive contents from locker
	Parameters  :
    * type (required) - Specifies the type of data to return. See the above list for valid types.
    * output (optional) - Your preferred output format. These include xml, and json.
    * count (optional) - Maximum number of results to be returned in a result set.
    * set (optional) - The result set number to retrieve, note that set numbers are 0 based.
    * album_id (optional) - Used when type=track to filter list of tracks by album. To retrieve a list of albums, set type=album.
    * artist_id (optional) - Used when type=album to filter list of albums by artist, or when type=track to filter list of tracks by artist. To retrieve a list of artists, set type=artist.
    * playlist_id (optional) - Used when type=track to filter list of tracks by playlist. To retrieve a list of playlists, set type=playlist or type=playmix.
    * noplaymix=1 (optional) - Used when type=playlist to exclude PlayMix lists. This would be used in combination with type=playmix in order to break out user created playlists separately from PlayMix lists.

**********************************************************/

	mp3tunes.prototype.mp3_getLockerData = function(type, count, set, album_id, artist_id, playlist_id, token, noplaymix,callback){
		var requestString = "http://ws.mp3tunes.com/api/v1/lockerData?output=json&sid=" + sid + "&type=" + type;
		if(count!="")
			requestString = requestString + "&count=" + count ;
		if(set!="")
			requestString = requestString + "&set=" + set;
		if(album_id!="")
			requestString = requestString + "&album_id=" + album_id;
		if(artist_id!="")
			requestString = requestString +"&artist_id=" + artist_id;
		if(playlist_id!="")
			requestString = requestString + "&playlist_id=" + playlist_id;
		if(token != "")
			requestString = requestString + "&token=" + token;
		if(noplaymix!="")
			requestString = requestString + "&noplaymix=1";
		requestString = requestString + "&callback=" + callback;

		this.sendRequest(requestString, false);
	}	 

/*********************************************************
The createAccount method creates a user account.
	Parameters  :
    * email (required) - Email address. Must be unique within the system
    * password (required) - Password
    * firstname (optional) - User’s given name
    * lastname (optional)- User’s family name
**********************************************************/
	mp3tunes.prototype.mp3_createAccount = function(email, password, firstname, lastname){
		var requestString = "http://ws.mp3tunes.com/api/v1/createAccount?output=json&email=" + email + "&password=" + password;
		if(firstname != "")
			requestString = requestString + "&firstname=" + firstname;
		if(lastname != "")
			requestString = requestString  + "&lastname=" + lastname;
		requestString = requestString + "&partner_token=2989248581&callback=mp3_newAccount";
		debug.trace(partnertoken);
		debug.trace(requestString);
	
		this.sendRequest(requestString, false);
	}

/************************************************************************************************************
	send XML Request
***************************************************************************************************************/

 mp3tunes.prototype.sendRequest = function(requestString, iscov){
	isCover = iscov;
	request = new XMLHttpRequest();
	if(request!=null){
		request.onreadystatechange = this.stateChange;
		request.open("GET",requestString,true);
		request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		request.send();
	} else {
		showAlert("connection failed" , request.status);
		request = null;
	}
}

 mp3tunes.prototype.stateChange = function(){
	if(request.readyState==4){
		if(request.status==200){
			if(isCover){
				imgAlbumcover.src = request.responseStream;
				imgAlbumcover.visible = true;
				imgAlbumcover.enabled = true;
			} else {
				var r = request.responseText;
				//debug.trace(r);
				eval("(" + r +")");
			}
		}else {
			showAlert("XML retrieving failed: ", request.status);
			request = null;
		}
	}
}

}

/**************************************************************
	set the session ID to the retrieved value
*************************************************************/
function mp3_setSessionID(obj){
	anim.stopLodingAnimation();
	if(obj.status == 1){
		sid = obj.session_id;
		loggedIn(true);
	}else{
		sid=null;
		loggedIn(false);
		alert(LOGIN_FAIL);
	}
}

/**************************************************************
	handel returnobject for new account
*************************************************************/
function mp3_newAccount(obj){
	anim.stopLodingAnimation();
	if(obj.status == 1){
		alert(ACCOUNT_CREATED);
	} else {
		if(obj.errorCode == 101){
			alert(EMAIL_EXIST);
		} else if (obj.errorCode == 102){
			alert(INVALID_PASSWORD);
		}
	}
}
